/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.vrangogym.beans.backing;

import edu.chl.vrangogym.persistence.entities.Booking;
import edu.chl.vrangogym.persistence.impl.BookingFacade;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author Anders
 */
@ManagedBean(name = "Schedule")
@RequestScoped
public class ScheduleBackingBean implements Serializable {
    @EJB
    private BookingFacade bookingFacade;

    private ScheduleModel bookingsModel;
    
    private enum CSS_STYLES {
        FEW("few"), MANY("many"), FULL("full");
        
        private final String stringRepresentation;
        
        CSS_STYLES(String stringRep) {
            this.stringRepresentation = stringRep;
        }
        
        @Override
        public String toString() {
            return stringRepresentation;
        }
    }
    private final int FEW = 1;
    private final int MANY = 5;
    private final int FULL = 8;

    public ScheduleBackingBean() {
    }
    
    @PostConstruct
    public void initSchedule() {
        // Get all future bookings
        List<Booking> bookings = bookingFacade.findAll(Calendar.getInstance().getTime());
        
        // Populate the BookingsModel
        bookingsModel = new DefaultScheduleModel();
        for(Booking b : bookings) {
            DefaultScheduleEvent dse = new DefaultScheduleEvent(
                    String.valueOf(b.getParticipants()),
                    b.getWorkoutSession().getStartTime(),
                    b.getWorkoutSession().getEndTime(), false);
            int participants = b.getParticipants();
            if(participants >= FULL) {
                dse.setStyleClass(CSS_STYLES.FULL.toString());
            } else if(participants >= MANY) {
                dse.setStyleClass(CSS_STYLES.MANY.toString());
            } else if(participants >= FEW) {
                dse.setStyleClass(CSS_STYLES.FEW.toString());
            }
            bookingsModel.addEvent(dse);
            System.err.println("StartTime" + b.getWorkoutSession().getStartTime());
        }
    }

    public ScheduleModel getBookingsModel() {
        return bookingsModel;
    }

    public void setBookingsModel(ScheduleModel bookingsModel) {
        this.bookingsModel = bookingsModel;
    }
    
}
